package top.carrotguo.weibo.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.annotation.NonNull;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ImageSpan;
import android.view.View;
import android.widget.TextView;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import top.carrotguo.weibo.R;

/**
 * Created by carrot on 2018-10-29.
 */

public class StringUtils {

    private static Context mContext;

    public static boolean isEmpty(String str){
        if (str != null
                && !"".equalsIgnoreCase(str.trim())
                && !"null".equalsIgnoreCase(str.trim())) {
            return false;
        }
        return true;
    }

    public static SpannableString getContentText(Context context, TextView tv, String content){
        mContext = context;
        /*匹配@+用户名*/
        String regexAt = "@[\u4e00-\u9fa5\\w]+";
        /*匹配话题*/
        String regexTopic = "#[\u4e00-\u9fa5\\w]+#";
        /*匹配表情*/
        String regexEmoji = "\\[[\u4e00-\u9fa5\\w]+\\]";

        /*三个组的匹配*/
        String regex = "("+regexAt+")|("+regexTopic+")|("+regexEmoji+")";

        /*富文本*/
        SpannableString spannableString = new SpannableString(content);

        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(spannableString);

        //若有匹配结果，则设置textview有超链接
        if (matcher.find()) {
            tv.setMovementMethod(LinkMovementMethod.getInstance());
            //回到原始标记位置
            matcher.reset();
        }


        while (matcher.find()) {
            //第一个括号匹配的
            final String atStr = matcher.group(1);
            /*第二个括号匹配的*/
            final String topicStr = matcher.group(2);
            String emojiStr = matcher.group(3);

            if (atStr != null) {
                //开启富文本的位置
                int start = matcher.start(1);

                BoreClickableSpan span = new BoreClickableSpan(){
                    @Override
                    public void onClick(@NonNull View widget) {
                        ToastUtils.shortToast(mContext,"用户："+atStr);
                    }
                };
                //设置富文本
                spannableString.setSpan(span,start,start+atStr.length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }

            if (topicStr != null) {
                int start = matcher.start(2);

                BoreClickableSpan span = new BoreClickableSpan(){
                    @Override
                    public void onClick(@NonNull View widget) {
                        ToastUtils.shortToast(mContext,"话题："+topicStr);
                    }
                };

                spannableString.setSpan(span,start,start+topicStr.length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }

            if (emojiStr != null) {
                int start = matcher.start(3);
                int imgRes = EmojiUtils.getImgByName(emojiStr);
                Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(),imgRes);
                if (bitmap != null) {
                    int size = (int) tv.getTextSize();
                    bitmap = Bitmap.createScaledBitmap(bitmap,size,size,true);
                    ImageSpan span = new ImageSpan(mContext,bitmap);
                    spannableString.setSpan(span,start,start+emojiStr.length(),Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                }
            }
        }
        return spannableString;
    }

    /**
     * 可点击的富文本，并且修改文字颜色
     */
    static class BoreClickableSpan extends ClickableSpan {

        @Override
        public void onClick(@NonNull View widget) {

        }

        @Override
        public void updateDrawState(@NonNull TextPaint ds) {
            //设置字体颜色
            ds.setColor(mContext.getResources().getColor(R.color.text_blue));
            //设置无下划线
            ds.setUnderlineText(false);
        }
    }

}
